package com.dragon.common.utils.html;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.common.constant.Constants;

public class XmlUtil {
	public static List<JSONObject> xmlToJsons(String xmlStr){
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlStr);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();// 指向根节点
        Iterator iterator = root.elementIterator();
        List<JSONObject> resultList = new LinkedList<>();
        while (iterator.hasNext()){
            Element stu = (Element) iterator.next();
            String name = stu.getName();
            if(name.equals("head")) continue;
            Iterator iterator1 = stu.elementIterator();
            while (iterator1.hasNext()){
            	Element stu1 = (Element) iterator1.next();
            	List<Attribute> attributes = stu1.attributes();
                JSONObject result = new JSONObject();
                for (Attribute attribute : attributes) {
                	result.put(attribute.getName(), attribute.getValue());
                }
                Iterator iterator2 = stu1.elementIterator();
                while (iterator2.hasNext()){
                	Element stu2 = (Element) iterator2.next();
                	List<Attribute> attributes2 = stu2.attributes();
                    JSONObject row = new JSONObject();
                    for (Attribute attribute2 : attributes2) {
                    	row.put(attribute2.getName(), attribute2.getValue());
                    }
                    result.put(stu2.getName(), row);
                }
                resultList.add(result);
            }
        }
		return resultList;
	}
	public static List<JSONObject> xmlToJSONList(String xmlStr) {
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlStr);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();// 指向根节点

        Iterator iterator = root.elementIterator();
        List<JSONObject> resultList = new LinkedList<>();
        while (iterator.hasNext()){
            Element stu = (Element) iterator.next();
            List<Attribute> attributes = stu.attributes();
            JSONObject result = new JSONObject();
            for (Attribute attribute : attributes) {
            	result.put(attribute.getName(), attribute.getValue());
            }
            Iterator iterator1 = stu.elementIterator();
            List<JSONObject> rows = new LinkedList<>();
            while (iterator1.hasNext()){
            	Element stu1 = (Element) iterator1.next();
                List<Attribute> attributes1 = stu1.attributes();
                JSONObject row = new JSONObject();
                for (Attribute attribute1 : attributes1) {
                	row.put(attribute1.getName(), attribute1.getValue());
                }
                rows.add(row);
            }
            result.put("rows", rows);
            resultList.add(result);
        }
		return resultList;
	}
	
	public static JSONArray packDggpData(JSONArray list1){
		return list1;
	}
	
	public static List<JSONObject> packDggpData(List<JSONObject> list1,List<JSONObject> list2,List<JSONObject> list3,List<JSONObject> list4) {
		List<JSONObject> resultList = new LinkedList<>();
		JSONObject json1;
		JSONObject json2;
		JSONObject json3;
		DecimalFormat df = new DecimalFormat("######0.00");
		for(int i=0; i<list1.size(); i++) {
			json1 = list1.get(i);
			String id = json1.getString("id");
			String processname = json1.getString("processname");
			if(list2!=null) {
				Iterator<JSONObject> iterator = list2.iterator();
				while (iterator.hasNext()) {
					json2 = iterator.next();
			        if (id.equals(json2.getString("id"))) {
			        	json1.put("spf", packDggpData(json2.getJSONArray("rows")));
			        	iterator.remove();//使用迭代器的删除方法删除
			        }
			     }	
			}
	        if(list3!=null) {
	        	Iterator<JSONObject> iterator1 = list3.iterator();
		        while (iterator1.hasNext()) {
		        	json2 = iterator1.next();
		            if (id.equals(json2.getString("id"))) {
		            	json1.put("nspf", packDggpData(json2.getJSONArray("rows")));
		            	iterator1.remove();//使用迭代器的删除方法删除
		            }
		        }
	        }
	        Iterator<JSONObject> iterator2 = list4.iterator();
	        while (iterator2.hasNext()) {
	        	json3 = iterator2.next();
	            if (processname.equals(json3.getString("processname"))) {
	            	json1.put("odds", json3);
	            	List<JSONObject> EventHedgeProfitsSingle = new LinkedList<>();
	            	JSONObject europe = json3.getJSONObject("europe");
	            	String hg = europe.getString("hg");
	            	String[] typeList = new String[] {"生死平","生死1","生死2"};
	            	JSONArray nspf = json1.getJSONArray("nspf");
	            	JSONArray spf = json1.getJSONArray("spf");
	            	JSONObject ehps;
	            	List<String> pk_ = new ArrayList<>();
	            	List<JSONObject> EventPlateSingleDetailInfos = new ArrayList<JSONObject>();
		        	JSONObject EventPlateSingleDetailInfo = new JSONObject();
		        	EventPlateSingleDetailInfo.put("Website", json1.getString("LeagueName"));
		        	EventPlateSingleDetailInfo.put("Plate", "盘口");
		        	EventPlateSingleDetailInfo.put("HomeFieldWater", json1.getString("HomeField"));
		        	EventPlateSingleDetailInfo.put("FlatWater", "平局");
		        	EventPlateSingleDetailInfo.put("OpponentFieldWater", json1.getString("OpponentField"));
		        	EventPlateSingleDetailInfos.add(EventPlateSingleDetailInfo);
		        	for(int k=0; k<nspf.size(); k++) {
		        		JSONObject r_ = nspf.getJSONObject(k);
		        		if(r_.getInteger("w")!=0||r_.getInteger("l")!=0||r_.getInteger("d")!=0) continue;
		        		r_.put("Plate", "+0**");
		        		r_.put("Website", "彩");
		        		r_.put("HomeFieldWater", r_.get("win"));
		        		r_.put("FlatWater", r_.get("draw"));
		        		r_.put("OpponentFieldWater", r_.get("lost"));
		        		EventPlateSingleDetailInfos.add(r_);
		        	}
		        	for(int k=0; k<spf.size(); k++) {
		        		JSONObject r_ = spf.getJSONObject(k);
		        		if(r_.getInteger("w")!=0||r_.getInteger("l")!=0||r_.getInteger("d")!=0) continue;
		        		r_.put("Plate", "+0**");
		        		r_.put("Website", "彩");
		        		r_.put("HomeFieldWater", r_.get("win"));
		        		r_.put("FlatWater", r_.get("draw"));
		        		r_.put("OpponentFieldWater", r_.get("lost"));
		        		EventPlateSingleDetailInfos.add(r_);
		        	}
		        	JSONObject europeHg = new JSONObject();
		        	String[] ehgs = hg.split(",");
		        	europeHg.put("Plate", "+0**");
		        	europeHg.put("Website", "HG");
		        	europeHg.put("HomeFieldWater", ehgs[0]);
		        	europeHg.put("FlatWater", ehgs[1]);
		        	europeHg.put("OpponentFieldWater", ehgs[2]);
	        		EventPlateSingleDetailInfos.add(europeHg);
		        	String[] pageIds = HtmlUtil.getPageIds();
		        	String SerialNumber = json1.getString("SerialNumber");
		        	for(String pageId : pageIds) {
		        		if(pageId.indexOf(SerialNumber)<0) continue;
		        		String id_ = pageId.split("\\^")[0];
		        		List<List<String>> pkoods = HtmlUtil.getpkodds(id_);
		        		for(List<String> pk : pkoods) {
		        			JSONObject r_ = new JSONObject();
		        			r_.put("Plate", pk.get(1));
			        		r_.put("Website", "HG");
			        		r_.put("HomeFieldWater", pk.get(0));
			        		r_.put("FlatWater", "");
			        		r_.put("OpponentFieldWater", pk.get(2));
			        		if(pk.get(1).equals("受让半球")) pk_= pk;
			        		EventPlateSingleDetailInfos.add(r_);
		        		}
		        	}
		        	json1.put("EventPlateSingleDetailInfos", EventPlateSingleDetailInfos);
		        	boolean isZ = true;
		        	for(int o=0; o<typeList.length; o++) {
	            		String type_ = typeList[o];
	            		if(o==0) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("主J +0**："+win);
        						jcplValues.add(Double.valueOf(win));
        						spfStr.add(win+","+nf.getString("draw")+","+lost);
        						jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			ehps.put("FlatPlateOdds", "H +0**："+hg.split(",")[1]);
		            			String PeripheryPlateOdds = hg.split(",")[2];
		            			if(jcplNames.get(k).indexOf("主")<0) {
		            				PeripheryPlateOdds = hg.split(",")[0];
		            				isZ = false;
		            			} else {
		            				isZ = true;
		            			}
		            			ehps.put("PeripheryPlateOdds", "H +0**："+PeripheryPlateOdds);
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(hg.split(",")[1]), Double.valueOf(PeripheryPlateOdds));
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(PeripheryPlateOdds);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(hg.split(",")[1]);
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
		            			if(isZ) {
		            				ehps = new JSONObject();
		            				ehps.put("Type", type_);
		            				ehps.put("HomeField", json1.getString("HomeField"));
			            			ehps.put("OpponentField", json1.getString("OpponentField"));
			            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
			            			PeripheryPlateOdds = pk_.get(2);
			            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
			            			ehps.put("FlatPlateOdds", "H +0**："+hg.split(",")[1]);
			            			RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(hg.split(",")[1]), Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("RewordReturn", RewordReturn);
			            			BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
			            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
			            			BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(hg.split(",")[1]);
			            			ehps.put("BetMoneyFlat", BetMoneyFlat);
			            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
			            			income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
			            			ehps.put("Income1", income1);
			            			EventHedgeProfitsSingle.add(ehps);
		            			}
	            			}
	            		}else if(o==1) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			ehps.put("FlatPlateOdds", "");
		            			String PeripheryPlateOdds = pk_.get(0);
		            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(PeripheryPlateOdds)+1d);
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = 0d;
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
	            			}
	            		}else if(o==2) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("主J +0**："+win);
        						jcplValues.add(Double.valueOf(win));
        						spfStr.add(win+","+nf.getString("draw")+","+lost);
        						jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			String PeripheryPlateOdds = hg.split(",")[2];
		            			if(jcplNames.get(k).indexOf("主")<0) {
		            				PeripheryPlateOdds = hg.split(",")[0];
		            				isZ = false;
		            			} else {
									isZ = true;
								}
		            			ehps.put("PeripheryPlateOdds", "H +0**："+PeripheryPlateOdds);
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			ehps.put("FlatPlateOdds", "J +0**："+spfList_[1]);
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(spfList_[1]), Double.valueOf(PeripheryPlateOdds));
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(PeripheryPlateOdds);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(spfList_[1]);
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.jcReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
		            			if(isZ) {
		            				ehps = new JSONObject();
		            				ehps.put("Type", type_);
		            				ehps.put("HomeField", json1.getString("HomeField"));
			            			ehps.put("OpponentField", json1.getString("OpponentField"));
			            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
			            			PeripheryPlateOdds = pk_.get(2);
			            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
			            			ehps.put("FlatPlateOdds", "J +0**："+spfList_[1]);
			            			RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(spfList_[1]), Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("RewordReturn", RewordReturn);
			            			BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
			            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
			            			BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(spfList_[1]);
			            			ehps.put("BetMoneyFlat", BetMoneyFlat);
			            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
			            			income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.jcReturn;
			            			ehps.put("Income1", income1);
			            			EventHedgeProfitsSingle.add(ehps);
		            			}
	            			}
	            		}
	            	}
	            	json1.put("EventHedgeProfitsSingle", EventHedgeProfitsSingle);
	            	
	                iterator2.remove();//使用迭代器的删除方法删除
	            }
	        }
			resultList.add(json1);
		}
		System.out.println(resultList);
		return resultList;
	}
	
	public static List<JSONObject> ecypackDggpData(List<JSONObject> list1,List<JSONObject> list2,List<JSONObject> list3,List<JSONObject> list4) {
		List<JSONObject> resultList = new LinkedList<>();
		JSONObject json1;
		JSONObject json2;
		JSONObject json3;
		DecimalFormat df = new DecimalFormat("######0.00");
		for(int i=0; i<list1.size(); i++) {
			json1 = list1.get(i);
			String id = json1.getString("id");
			String processname = json1.getString("processname");
			if(list2!=null) {
				Iterator<JSONObject> iterator = list2.iterator();
				while (iterator.hasNext()) {
					json2 = iterator.next();
			        if (id.equals(json2.getString("id"))) {
			        	json1.put("spf", packDggpData(json2.getJSONArray("rows")));
			        	iterator.remove();//使用迭代器的删除方法删除
			        }
			     }	
			}
	        if(list3!=null) {
	        	Iterator<JSONObject> iterator1 = list3.iterator();
		        while (iterator1.hasNext()) {
		        	json2 = iterator1.next();
		            if (id.equals(json2.getString("id"))) {
		            	json1.put("nspf", packDggpData(json2.getJSONArray("rows")));
		            	iterator1.remove();//使用迭代器的删除方法删除
		            }
		        }
	        }
	        Iterator<JSONObject> iterator2 = list4.iterator();
	        while (iterator2.hasNext()) {
	        	json3 = iterator2.next();
	            if (processname.equals(json3.getString("processname"))) {
	            	json1.put("odds", json3);
	            	List<JSONObject> EventHedgeProfitsSingle = new LinkedList<>();
	            	JSONObject europe = json3.getJSONObject("europe");
	            	String hg = europe.getString("hg");
	            	String[] typeList = new String[] {"生死平","生死1","生死2"};
	            	JSONArray nspf = json1.getJSONArray("nspf");
	            	JSONArray spf = json1.getJSONArray("spf");
	            	JSONObject ehps;
	            	List<String> pk_ = new ArrayList<>();
	            	List<JSONObject> EventPlateSingleDetailInfos = new ArrayList<JSONObject>();
		        	JSONObject EventPlateSingleDetailInfo = new JSONObject();
		        	EventPlateSingleDetailInfo.put("Website", json1.getString("LeagueName"));
		        	EventPlateSingleDetailInfo.put("Plate", "盘口");
		        	EventPlateSingleDetailInfo.put("HomeFieldWater", json1.getString("HomeField"));
		        	EventPlateSingleDetailInfo.put("FlatWater", "平局");
		        	EventPlateSingleDetailInfo.put("OpponentFieldWater", json1.getString("OpponentField"));
		        	EventPlateSingleDetailInfos.add(EventPlateSingleDetailInfo);
		        	for(int k=0; k<nspf.size(); k++) {
		        		JSONObject r_ = nspf.getJSONObject(k);
		        		if(r_.getInteger("w")!=0||r_.getInteger("l")!=0||r_.getInteger("d")!=0) continue;
		        		r_.put("Plate", "+0**");
		        		r_.put("Website", "彩");
		        		r_.put("HomeFieldWater", r_.get("win"));
		        		r_.put("FlatWater", r_.get("draw"));
		        		r_.put("OpponentFieldWater", r_.get("lost"));
		        		EventPlateSingleDetailInfos.add(r_);
		        	}
		        	for(int k=0; k<spf.size(); k++) {
		        		JSONObject r_ = spf.getJSONObject(k);
		        		if(r_.getInteger("w")!=0||r_.getInteger("l")!=0||r_.getInteger("d")!=0) continue;
		        		r_.put("Plate", "+0**");
		        		r_.put("Website", "彩");
		        		r_.put("HomeFieldWater", r_.get("win"));
		        		r_.put("FlatWater", r_.get("draw"));
		        		r_.put("OpponentFieldWater", r_.get("lost"));
		        		EventPlateSingleDetailInfos.add(r_);
		        	}
		        	JSONObject europeHg = new JSONObject();
		        	String[] ehgs = hg.split(",");
		        	europeHg.put("Plate", "+0**");
		        	europeHg.put("Website", "HG");
		        	europeHg.put("HomeFieldWater", ehgs[0]);
		        	europeHg.put("FlatWater", ehgs[1]);
		        	europeHg.put("OpponentFieldWater", ehgs[2]);
	        		EventPlateSingleDetailInfos.add(europeHg);
		        	String[] pageIds = HtmlUtil.getPageIds();
		        	String SerialNumber = json1.getString("SerialNumber");
		        	for(String pageId : pageIds) {
		        		if(pageId.indexOf(SerialNumber)<0) continue;
		        		String id_ = pageId.split("\\^")[0];
		        		List<List<String>> pkoods = HtmlUtil.getpkodds(id_);
		        		for(List<String> pk : pkoods) {
		        			JSONObject r_ = new JSONObject();
		        			r_.put("Plate", pk.get(1));
			        		r_.put("Website", "HG");
			        		r_.put("HomeFieldWater", pk.get(0));
			        		r_.put("FlatWater", "");
			        		r_.put("OpponentFieldWater", pk.get(2));
			        		if(pk.get(1).equals("受让半球")) pk_= pk;
			        		EventPlateSingleDetailInfos.add(r_);
		        		}
		        	}
		        	json1.put("EventPlateDoubleToOneDetailInfos", EventPlateSingleDetailInfos);
		        	boolean isZ = true;
		        	for(int o=0; o<typeList.length; o++) {
	            		String type_ = typeList[o];
	            		if(o==0) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("主J +0**："+win);
        						jcplValues.add(Double.valueOf(win));
        						spfStr.add(win+","+nf.getString("draw")+","+lost);
        						jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("SerialNumber", json1.getString("SerialNumber"));
		            			ehps.put("LeagueName", json1.getString("LeagueName"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			ehps.put("FlatPlateOdds", "H +0**："+hg.split(",")[1]);
		            			String PeripheryPlateOdds = hg.split(",")[2];
		            			if(jcplNames.get(k).indexOf("主")<0) {
		            				PeripheryPlateOdds = hg.split(",")[0];
		            				isZ = false;
		            			} else {
		            				isZ = true;
		            			}
		            			ehps.put("PeripheryPlateOdds", "H +0**："+PeripheryPlateOdds);
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(hg.split(",")[1]), Double.valueOf(PeripheryPlateOdds));
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(PeripheryPlateOdds);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(hg.split(",")[1]);
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
		            			if(isZ&&pk_.size()>0) {
		            				ehps = new JSONObject();
		            				ehps.put("Type", type_);
		            				ehps.put("HomeField", json1.getString("HomeField"));
			            			ehps.put("OpponentField", json1.getString("OpponentField"));
			            			ehps.put("SerialNumber", json1.getString("SerialNumber"));
			            			ehps.put("LeagueName", json1.getString("LeagueName"));
			            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
			            			PeripheryPlateOdds = pk_.get(2);
			            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
			            			ehps.put("FlatPlateOdds", "H +0**："+hg.split(",")[1]);
			            			RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(hg.split(",")[1]), Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("RewordReturn", RewordReturn);
			            			BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
			            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
			            			BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(hg.split(",")[1]);
			            			ehps.put("BetMoneyFlat", BetMoneyFlat);
			            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
			            			income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
			            			ehps.put("Income1", income1);
			            			EventHedgeProfitsSingle.add(ehps);
		            			}
	            			}
	            		}else if(o==1 && pk_.size()>0) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("SerialNumber", json1.getString("SerialNumber"));
		            			ehps.put("LeagueName", json1.getString("LeagueName"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			ehps.put("FlatPlateOdds", "");
		            			String PeripheryPlateOdds = pk_.get(0);
		            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(PeripheryPlateOdds)+1d);
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = 0d;
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.hgReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
	            			}
	            		}else if(o==2) {
	            			List<String> jcplNames = new LinkedList<>();
	            			List<Double> jcplValues = new LinkedList<>();
	            			List<String> spfStr = new LinkedList<>();
	            			for(int l=0; l<nspf.size(); l++) {
	            				JSONObject nf = nspf.getJSONObject(l);
	            				Integer l_ = nf.getInteger("l");
	            				Integer w_ = nf.getInteger("w");
	            				Integer d_ = nf.getInteger("d");
	            				if(w_!=0||l_!=0||d_!=0) continue;
	            				String win = nf.getString("win");
	            				String lost = nf.getString("lost");
	            				jcplNames.add("主J +0**："+win);
        						jcplValues.add(Double.valueOf(win));
        						spfStr.add(win+","+nf.getString("draw")+","+lost);
        						jcplNames.add("客J +0**："+lost);
        						jcplValues.add(Double.valueOf(lost));
        						spfStr.add(lost+","+nf.getString("draw")+","+win);
	            			}
	            			for(int k=0; k<jcplNames.size(); k++) {
	            				ehps = new JSONObject();
	            				ehps.put("Type", type_);
	            				ehps.put("HomeField", json1.getString("HomeField"));
		            			ehps.put("OpponentField", json1.getString("OpponentField"));
		            			ehps.put("SerialNumber", json1.getString("SerialNumber"));
		            			ehps.put("LeagueName", json1.getString("LeagueName"));
		            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
		            			String PeripheryPlateOdds = hg.split(",")[2];
		            			if(jcplNames.get(k).indexOf("主")<0) {
		            				PeripheryPlateOdds = hg.split(",")[0];
		            				isZ = false;
		            			} else {
									isZ = true;
								}
		            			ehps.put("PeripheryPlateOdds", "H +0**："+PeripheryPlateOdds);
		            			String[] spfList_ = spfStr.get(k).split(",");
		            			ehps.put("FlatPlateOdds", "J +0**："+spfList_[1]);
		            			Double RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(spfList_[1]), Double.valueOf(PeripheryPlateOdds));
		            			ehps.put("RewordReturn", RewordReturn);
		            			Double BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(PeripheryPlateOdds);
		            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
		            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
		            			Double BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(spfList_[1]);
		            			ehps.put("BetMoneyFlat", BetMoneyFlat);
		            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
		            			Double income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.jcReturn;
		            			ehps.put("Income1", income1);
		            			EventHedgeProfitsSingle.add(ehps);
		            			if(isZ&& pk_.size()>0) {
		            				ehps = new JSONObject();
		            				ehps.put("Type", type_);
		            				ehps.put("HomeField", json1.getString("HomeField"));
			            			ehps.put("OpponentField", json1.getString("OpponentField"));
			            			ehps.put("LotteryPlateOdds", jcplNames.get(k));
			            			PeripheryPlateOdds = pk_.get(2);
			            			ehps.put("PeripheryPlateOdds", "H +0.5："+df.format(Double.valueOf(PeripheryPlateOdds)+1d));
			            			ehps.put("FlatPlateOdds", "J +0**："+spfList_[1]);
			            			RewordReturn = getReturnL(Double.valueOf(spfList_[0]), Double.valueOf(spfList_[1]), Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("RewordReturn", RewordReturn);
			            			BetMoneyPeriphery = Constants.TotalExpense*jcplValues.get(k)/(Double.valueOf(PeripheryPlateOdds)+1d);
			            			ehps.put("BetMoneyJingCai", Constants.TotalExpense);
			            			ehps.put("BetMoneyPeriphery", BetMoneyPeriphery);
			            			BetMoneyFlat = Constants.TotalExpense*jcplValues.get(k)/Double.valueOf(spfList_[1]);
			            			ehps.put("BetMoneyFlat", BetMoneyFlat);
			            			ehps.put("TotalExpense", Constants.TotalExpense+BetMoneyPeriphery+BetMoneyFlat);
			            			income1 = Constants.TotalExpense*jcplValues.get(k)+BetMoneyPeriphery*Constants.hgReturn+BetMoneyFlat*Constants.jcReturn;
			            			ehps.put("Income1", income1);
			            			EventHedgeProfitsSingle.add(ehps);
		            			}
	            			}
	            		}
	            	}
	            	json1.put("EventHedgeProfitsDoubleToSingle", EventHedgeProfitsSingle);
	            	
	                iterator2.remove();//使用迭代器的删除方法删除
	            }
	        }
			resultList.add(json1);
		}
		System.out.println(resultList);
		return resultList;
	}
	
	public static Double getReturnL(Double a, Double b, Double c) {
		return a*b*c/(a*b+a*c+b*c);
	}
	
	public static Double getReturnL(Double a, Double b) {
		return a*b/(a+b);
	}
}
